package de.tc.stammdatenverwaltung.mailing;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Vector;
import java.util.Properties;
import java.util.Iterator;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.Store;
import javax.mail.Folder;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;

public class mailSender
{
  public static void postMail( Vector<String> recipients, String subject, String message, String from,
    String server, String port, String user, String pass, boolean auth, int authArt, boolean htmlMail)
      throws MessagingException
  {
    Properties props = new Properties();
    props.put( "mail.smtp.host", server );
    props.put( "mail.smtp.port", port);
    props.put( "mail.user", user);
    props.put( "mail.smtp.auth", auth);
    props.put( "mail.transport.protocol", "smtp");
    
    if (authArt == 1)
      props.put( "mail.smtp.starttls.enable", true);
    else if (authArt == 2)
      props.put( "mail.smtp.ssl.enable", true);
    
    Session session = Session.getDefaultInstance( props );
    InternetAddress addressFrom = new InternetAddress( from );
    
    Message msg = new MimeMessage( session );
    msg.setFrom( addressFrom );

    boolean first = true;
    for (Iterator<String> it = recipients.iterator(); it.hasNext();)
    {
      String recipient = it.next();
      if ( (recipient != null) && ! recipient.isEmpty() )
      {
        if (first)
        {
          msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient) );
          first = false;
        }
        else
          msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(recipient) );
      }// !null; !empty
    }//for

    msg.setSubject( subject );
    if ( htmlMail )
      msg.setContent( message, "text/html" );
    else
      msg.setContent( message, "text/plain" );

    msg.saveChanges(); // don't forget this
    
    // Send message with authentication!
    Transport tr = session.getTransport("smtp");
    tr.connect(server, user, pass);
    tr.sendMessage(msg, msg.getAllRecipients());
    tr.close();
  }

  public static void getPopMail( String host, String user, String passwd ) throws Exception
  {
    Session session = Session.getDefaultInstance( new Properties() );
    Store store = session.getStore( "pop3" );
    store.connect( host, user, passwd );
    Folder folder = store.getFolder( "INBOX" );
    folder.open( Folder.READ_ONLY );
    Message[] message = folder.getMessages();
    for ( int i = 0; i < message.length; i++ )
    {
      Message m = message[i];
      System.out.println( "Nachricht: " + i );
      System.out.println( "From: " + m.getFrom()[0] );
      System.out.println( "Subject: " + m.getSubject() );
      Multipart mp = (Multipart) m.getContent();
      for ( int j = 0; j < mp.getCount(); j++ )
      {
        Part part = mp.getBodyPart( j );
        String disposition = part.getDisposition();
        if ( disposition == null )
        {
          MimeBodyPart mimePart = (MimeBodyPart)part;
          if ( mimePart.isMimeType("text/plain") )
          {
            BufferedReader in = new BufferedReader(
              new InputStreamReader(mimePart.getInputStream()) );
            for ( String line; (line=in.readLine()) != null; )
              System.out.println( line );
          }
        }
      }
    }
    folder.close( false );
    store.close();
  }
}